<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>茶园</title>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/style/css/amazeui.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/style/css/app.css">
<link
	href="${pageContext.request.contextPath}/style/css/bootstrap.min.css"
	rel="stylesheet">
<link
	href="${pageContext.request.contextPath}/style/layui/css/layui.css"
	rel="stylesheet">
<script src="${pageContext.request.contextPath}/style/js/jquery.min.js"></script>
<script
	src="${pageContext.request.contextPath}/style/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/style/js/amazeui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/style/layui/layui.js"></script>
<style type="text/css">
label {
	font-family: "Times New Roman", Times, serif;
	font-size: 15px;
	color: #000000;
}

th {
	text-align: center;
}
</style>
</head>

<body>
	<div
		style="width:100%;background-color:#F8F8FF;height:100%;position: absolute;top:1px;left:1px">
		<div id="operatediv"
			style="height:7%;width:100%;position: absolute;top:10px;left:15px">
			<button id="refrsh" class="am-btn am-btn-primary am-btn-xs"
				onclick="history.go(0)" type="button">
				<i class="glyphicon glyphicon-refresh"></i>刷新
			</button>
			<button id="add" class="am-btn am-btn-primary am-btn-xs"
				type="button">
				<i class="glyphicon glyphicon-plus"></i>添加
			</button>
			<button id="edit" class="am-btn am-btn-primary am-btn-xs"
				type="button">
				<i class="am-icon-pencil"></i>编辑
			</button>
			<button id="medcine" class="am-btn am-btn-primary am-btn-xs"
				type="button">
				<i class="am-icon-pencil"></i>施肥、农药
			</button>

			<button id="trim" class="am-btn am-btn-primary am-btn-xs"
				type="button">
				<i class="am-icon-pencil"></i>除草、修剪
			</button>
			<button id="export" class="am-btn am-btn-primary am-btn-xs"
				type="button">
				<i class="am-icon-download"></i>茶园导出
			</button>
		</div>
		<div id="tablediv"
			style="height:70%;width:100%;position: absolute;top:40px;left:15px">
			<table class="layui-table" id="table1" border="1px">

				<thead>
					<tr>
						<th width="5px"><input id='allid' type='checkbox'></th>
						<th>编号</th>
						<th>茶园名</th>

						<th>主要品种</th>
						<th>主要负责人</th>
						<th>产期（月）</th>
						<th>面积（㎡）</th>
						<th>状态</th>
						<th>地址</th>

					</tr>
				<tbody id="table">


				</tbody>



			</table>
			<div id="page" style="text-align:center"></div>
		</div>


	</div>

</body>
<script type="text/javascript">
	$(document).ready(function() {
		isCheckAll = false;
		$("#allid").prop("checked", false);
		//ajax请求后台数据
		getlist();
		//点击搜索时 搜索数据
		$("#search").click(function() {
			getlist();
			currentPageAllAppoint = 1; //当点击搜索的时候，应该回到第一页
			toPage();//然后进行分页的初始化

		})
		toPage();
	});

	//分页参数设置 这些全局变量关系到分页的功能
	var startAllAppoint = 0;//开始页数
	var limitAllAppoint = 8;//每页显示数据条数
	var currentPageAllAppoint = 1;//当前页数
	var dataLength = 0;//数据总条数
	//ajax请求后台数据
	function getlist() {
		$.ajax({
			type : "post",
			async : false,
			dataType : "json",
			url : "${pageContext.request.contextPath}/land/list",
			data : {
				start : startAllAppoint,
				limit : limitAllAppoint
			},
			success : function(data, status) {
				gettable(data.root);
				startAllAppoint = data.currentResult;//当前页数(后台返回)
				dataLength = data.total;//数据总条数
			}
		});

	}
	function gettable(data) {
		var s = "";
		$
				.each(
						data,
						function(v, o) {
							//alert(JSON.stringify(o));
							s += "<tr><td><input type='checkbox' value="+o.landId +" ></td><td>"
									+ o.landId + "</td>";
							s += "<td>" + o.landName + "</td>";
							s += "<td>" + o.landVariety + "</td><td>"
									+ o.user.userName + "</td><td>"
									+ o.landTime + "</td><td>"+o.landArea+"</td>"
							if (o.landState == 1) {
								s += "<td>正常</td>";
							} else {
								s += "<td>关闭</td>";
							}
							s += "<td>" + o.landSite + "</td>";
						});
		if (data.length > 0) {
			$("#table").html(s);
		} else {
			$("#page").hide();
			$("#table")
					.html(
							"<br/><span style='width:10%;height:30px;display:block;margin:0 auto;'>暂无数据</span>");
		}

	}

	function toPage() {

		layui
				.use(
						[ 'form', 'laypage', 'layedit', 'layer', 'laydate' ],
						function() {
							var form = layui.form, layer = layui.layer, layedit = layui.layedit, laydate = layui.laydate, laypage = layui.laypage;

							var nums = 10;
							//调用分页
							laypage.render({
								elem : 'page',
								count : dataLength //这个是后台返回的数据的总条数
								,
								limit : limitAllAppoint //每页显示的数据的条数,layui会根据count，limit进行分页的计算
								,
								curr : currentPageAllAppoint,
								skip : true,
								jump : function(obj, first) {

									currentPageAllAppoint = obj.curr;
									startAllAppoint = (obj.curr - 1)
											* obj.limit;
									//document.getElementById('biuuu_city_list').innerHTML = render(obj, obj.curr);
									if (!first) { //一定要加此判断，否则初始时会无限刷新
										getlist();//一定要把翻页的ajax请求放到这里，不然会请求两次。
										//location.href = '?page='+obj.curr;
									}
								}
							});

						});
	};

	var isCheckAll = false;
	$("#allid").click(function() {
		if (!isCheckAll) {
			$("input[type='checkbox']").each(function() {
				$("input[type='checkbox']").prop("checked", true);

			});
			isCheckAll = true;
		} else {
			$("input[type='checkbox']").each(function() {
				$("input[type='checkbox']").prop("checked", false);
				isCheckAll = false;
			});
		}
	});

	$("input[type='checkbox']").click(function() {
		if ($(this).is(':checked')) {
		} else {
			isCheckAll = false;
			$("#allid").prop("checked", false);

		}
	});
	
	$("#export").click(function() {
		
		var myurl = "${pageContext.request.contextPath}/land/export";
		var form = $("<form>");
		form.attr("style", "display:none");
		form.attr("method", "post");
		form.attr("action", myurl);
		$("body").append(form);
		form.submit();
	});
</script>

<script type="text/javascript">
	layui.use([ 'layer', 'table', 'laypage' ], function() {
		var layer = layui.layer //获得layer模块
		, table = layui.table, page = layui.laypage; //获得laypage模块

		//使用模块
		$("#add").click(function() {

			layer.open({
				type : 2,
				title : '添加茶园',
				content : [ 'add' ],
				area : [ '500px', '400px' ],
				end : function() {
					location.reload();
				}
			});
		});

		$("#edit").click(function() {
			var len = 0;
			var id = 0;
			$.each($('input:checkbox:checked'), function() {
				// alert($('input[type=checkbox]:checked').length+","+$(this).val());
				len = $('input[type=checkbox]:checked').length;
				id = $(this).val();
			});
			if (len > 1 || len < 1) {
				layer.open({
					title : '茶园编辑',
					content : '请选择一条数据'
				});
			} else {
				layer.open({
					type : 2,
					title : '茶园编辑',
					content : [ 'edit?id=' + id ],
					area : [ '500px', '400px' ],
					end : function() {
						location.reload();
					}

				});
			}
		});

		$("#medcine").click(function() {
			var len = 0;
			var id = 0;
			$.each($('input:checkbox:checked'), function() {
				// alert($('input[type=checkbox]:checked').length+","+$(this).val());
				len = $('input[type=checkbox]:checked').length;
				id = $(this).val();
			});
			if (len > 1 || len < 1) {
				layer.open({
					title : '施肥、农药管理',
					content : '请选择一条数据'
				});
			} else {
				var tbodyObj = document.getElementById('table1');
		        $("table :checkbox").each(function(key,value){
		            if($(value).prop('checked')){
		            	var ids=tbodyObj.rows[key].cells[1].innerHTML;
		            	var state=tbodyObj.rows[key].cells[6].innerHTML;
		                if(state=='关闭'){
		                	layer.open({
		    					title : '施肥、农药管理',
		    					content : '请选择正常状态的茶园'
		    				});
		                }else{
		                	window.location.href = "${pageContext.request.contextPath}/medcine/info?ids="+ids;
		                }
		            }
		        });
				
			}
		});
		
		$("#trim").click(function() {
			var len = 0;
			var id = 0;
			$.each($('input:checkbox:checked'), function() {
				// alert($('input[type=checkbox]:checked').length+","+$(this).val());
				len = $('input[type=checkbox]:checked').length;
				id = $(this).val();
			});
			if (len > 1 || len < 1) {
				layer.open({
					title : '除草、修剪管理',
					content : '请选择一条数据'
				});
			} else {
				var tbodyObj = document.getElementById('table1');
		        $("table :checkbox").each(function(key,value){
		            if($(value).prop('checked')){
		            	var ids=tbodyObj.rows[key].cells[1].innerHTML;
		            	var state=tbodyObj.rows[key].cells[6].innerHTML;
		                if(state=='关闭'){
		                	layer.open({
		    					title : '除草、修剪管理',
		    					content : '请选择正常状态的茶园'
		    				});
		                }else{
		                	window.location.href = "${pageContext.request.contextPath}/trim/info?ids="+ids;
		                }
		            }
		        });
				
			}
		});

	});
</script>

</html>
